package com.wa.my.handler;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DisconnectListener;
import com.wa.handler.WebSocketConnectHandler;
import com.wa.my.util.WebSocketUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public class SocketConnectHandler implements ConnectListener, DisconnectListener {

    private Logger logger = LoggerFactory.getLogger(WebSocketConnectHandler.class);

    //可以存储到redis中，设值缓存一天就可以了
    Set<String> caches = new HashSet<>();

    @Override
    public void onConnect(SocketIOClient socketIOClient) {
        HandshakeData handshakeData = socketIOClient.getHandshakeData();
        //获取参数
        String chanel = handshakeData.getSingleUrlParam("a");
        //获取参数b
        String userType = handshakeData.getSingleUrlParam("b");
        //获取命名空间名称
        String namespace = socketIOClient.getNamespace().getName();
        //获取头部信息
        handshakeData.getHttpHeaders();
        String cookies = handshakeData.getHttpHeaders().get("cookie");
        String token = WebSocketUtil.getHttpRequestToken(cookies);

        caches.add(socketIOClient.getSessionId().toString());
    }

    @Override
    public void onDisconnect(SocketIOClient socketIOClient) {
        caches.remove(socketIOClient.getSessionId().toString());
    }
}
